package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.LGuest;
import com.ruoyi.system.service.ILGuestService;
import com.ruoyi.system.vo.LGuestVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LMessageMapper;
import com.ruoyi.system.domain.LMessage;
import com.ruoyi.system.service.ILMessageService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 消息Service业务层处理
 *
 * @author zyb
 * @date 2022-01-02
 */
@Service
public class LMessageServiceImpl extends ServiceImpl<LMessageMapper, LMessage> implements ILMessageService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ILGuestService lGuestService;

    /**
     * 查询消息
     *
     * @param mid 消息主键
     * @return 消息
     */
    @Override
    public LMessage selectLMessageByMid(Long mid) {
        return baseMapper.selectLMessageByMid(mid);
    }

    /**
     * 查询消息列表
     *
     * @param lMessage 消息
     * @return 消息
     */
    @Override
    public List<LMessage> selectLMessageList(LMessage lMessage) {
        return baseMapper.selectLMessageList(lMessage);
    }

    /**
     * 新增消息
     *
     * @param lMessage 消息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLMessage(LMessage lMessage) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        lMessage.setUserid(loginUser.getUserId());
        lMessage.setCreateBy(String.valueOf(getUserId()));
        lMessage.setCreateTime(DateUtils.getNowDate());
        System.out.println(lMessage.getContent());
        if (lMessage.getTogid() == -1) {
            //向所有客户发送信息
            List<LGuest> list = redisCache.getCacheList("lGuest");
            if (list.size() == 0) {
                LGuestVo lGuest = new LGuestVo();
                lGuest.setClosed(0);
                list = lGuestService.selectLGuestList(lGuest);
                redisCache.setCacheList("lGuest", list);
            }
            try {
                for (LGuest guest : list) {
                    if (guest.getUserid() == null) continue;
                    LMessage messageParam = new LMessage();
                    BeanUtils.copyProperties(lMessage, messageParam);
                    messageParam.setTogid(guest.getUserid());
                    baseMapper.insert(messageParam);
                }
                return 1;
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
        } else {
            return baseMapper.insert(lMessage);
        }

    }

    /**
     * 修改消息
     *
     * @param lMessage 消息
     * @return 结果
     */
    @Override
    public int updateLMessage(LMessage lMessage) {
        lMessage.setUpdateBy(String.valueOf(getUserId()));
        lMessage.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(lMessage);
    }

    /**
     * 批量删除消息
     *
     * @param mids 需要删除的消息主键
     * @return 结果
     */
    @Override
    public int deleteLMessageByMids(Long[] mids) {
        return baseMapper.deleteLMessageByMids(mids);
    }

    /**
     * 删除消息信息
     *
     * @param mid 消息主键
     * @return 结果
     */
    @Override
    public int deleteLMessageByMid(Long mid) {
        return baseMapper.deleteById(mid);
    }
}
